//index.js
function e(e) {
  return e && e.__esModule ? e : {
    default: e
  };
}

var t = Object.assign || function(e) {
    for (var t = 1; t < arguments.length; t++) {
      var a = arguments[t];
      for (var i in a) Object.prototype.hasOwnProperty.call(a, i) && (e[i] = a[i]);
    }
    return e;
  },

  a = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(e) {
    return typeof e;
  } : function(e) {
    return e && "function" == typeof Symbol && e.constructor === Symbol && e !== Symbol.prototype ? "symbol" : typeof e;
  },

  i = e(require("../../../api/api.js")),
  o = e(require("../../../utils/util.js")),
  r = e(require("../../../utils/filter.js")),
  s = require("../../../config/constants.js"),
  n = require("../../../data/translate.js"),
  l = require("../../../data/translate_tag.js"),

  c = getApp();
// const Promise = require('../../../utils/promise.js');
var qcloud = require('../../../vendor/wafer2-client-sdk/index')
var config = require('../../../config')
var util = require('../../../utils/util.js')

// 显示繁忙提示
var showBusy = text => wx.showToast({
  title: text,
  icon: 'loading',
  duration: 10000
});

// 显示成功提示
var showSuccess = text => wx.showToast({
  title: text,
  icon: 'success'
});

// 显示失败提示
var showModel = (title, content) => {
  wx.hideToast();

  wx.showModal({
    title,
    content: JSON.stringify(content),
    showCancel: false
  });
};

Page({
  data: {
    // pic_name: "",
    image1: " ",
    loginUrl: config.service.loginUrl,
    requestUrl: config.service.requestUrl,
    tunnelUrl: config.service.tunnelUrl,
    uploadUrl: config.service.uploadUrl,
    tunnelStatus: 'closed',
    tunnelStatusText: {
      closed: '已关闭',
      connecting: '正在连接...',
      connected: '已连接'
    },
    imgUrl: '',
    availableTimeDatum: " "
  },

  chooseImage1: function() {
    var that = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          image1: res.tempFilePaths[0]

        })
        console.log(that.data.image1)
      }
    })
  },

  bindGetUserInfo: function(e) {

    showBusy('正在登录');
    var that = this;
    var userInfo = e.detail.userInfo;

    // 查看是否授权
    wx.getSetting({
      success: function(res) {
        if (res.authSetting['scope.userInfo']) {
          // 检查登录是否过期
          wx.checkSession({
            success: function() {
              if (wx.getStorageSync("openid")) {
                // 登录态未过期
                showSuccess('登录成功');
                console.log('登录未过期：', wx.getStorageSync("openid"));
              } else {
                qcloud.clearSession();
                // 没有openid，也需重新登录
                var options = {
                  encryptedData: e.detail.encryptedData,
                  iv: e.detail.iv,
                  userInfo: userInfo
                }
                // console.log("e.detail.encryptedData")
                that.getWxLogin(options);
              }
            },

            fail: function() {
              qcloud.clearSession();
              // 登录态已过期，需重新登录
              var options = {
                encryptedData: e.detail.encryptedData,
                iv: e.detail.iv,
                userInfo: userInfo
              }
              // console.log(e.detail.iv)
              that.getWxLogin(options);
            },
          });
        } else {
          showModel('用户未授权', e.detail.errMsg);
        }
      }
    });

  },

  getWxLogin: function(options) {
    var that = this;

    wx.login({
      success: function(loginResult) {
        var loginParams = {
          code: loginResult.code,
          encryptedData: options.encryptedData,
          iv: options.iv,
        }
        qcloud.requestLogin({
          loginParams,
          success() {
            showSuccess('登录成功');
            console.log('登录成功：', wx.getStorageSync("openid"));
          },
          fail(error) {
            showModel('登录失败', error)
            console.log('登录失败', error)
          }
        });
      },
      fail: function(loginError) {
        showModel('登录失败', loginError)
        console.log('登录失败', loginError)
      },
    });
  },


  formSubmit: function(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    var that = this
    // this.setData({
    //   pic_name: e.detail.value.student_id + '.jpg'
    // })
    // console.log(config.service.inputUrl + this.data.pic_name)
    // 上传图片
    wx.uploadFile({
      url: config.service.uploadUrl,
      filePath: that.data.image1,
      name: 'file',
      success: function(res) {
        showSuccess('上传图片成功')
        // console.log(config.service.uploadUrl)
        // console.log(that.data.image1)
        res = JSON.parse(res.data)
        console.log(res)
        //有了图片路径再入库
        wx.request({
          url: config.service.inputUrl,
          data: {
            cater_id: wx.getStorageSync("openid"),
            cater_name: e.detail.value.name,
            cater_gender: e.detail.value.gender,
            cater_cell: e.detail.value.cell,
            cater_from: e.detail.value.from,
            cater_location: e.detail.value.location,
            cater_label: e.detail.value.cater_label,
            personal_label: e.detail.value.personal_label,
            title1: e.detail.value.title1,
            project1: e.detail.value.project1,
            title2: e.detail.value.title2,
            project2: e.detail.value.project2,
            cater_introduction: e.detail.value.introduction,
            cater_pic_url: res.data.imgUrl
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function(res) {
            console.log(res.data)
          },
          complete: function(res) {
            console.log(res.data)
            console.log(this.url)
          },
        })
      },

      fail: function(e) {
        showModel('上传图片失败')
      }
    })
  },

  formReset: function() {
    console.log('form发生了reset事件'),
      //以下仅供测试返回的达人时间（以后再单独开发或使用插件）：
      this.getTeacherAvailableTime(4549902),
      console.log(this.availableTimeDatum)
  },

  //这里调用i.default.getTeacherAvailableTime接口，id取个固定值。
  getTeacherAvailableTime: function(e) {
    var t = this,
      a = wx.getStorageSync("token") || "",
      r = {
        start_time: o.default.getDateStr(1) + " 00:00",
        end_time: o.default.getDateStr(7) + " 23:59",
        i_token: a
      };
    i.default.getTeacherAvailableTime({
      query: {
        id: e
      },
      data: r,
      success: function(e) {
        if (e && e.data.data) {
          //zl：注意这里a和e表示的时间格式：
          console.log(e.data.data);
          var a = e.data.data.slice(0, 7);
          // console.log(a);
          //每天都分成48个半小时，按照大小定权重，进而决定深浅。
          a.forEach(t.handlerAvailableTime),
            t.setData({
              availableTimeDatum: a
            });
        }
      }
    });
  },

  //zl: 这里的t和a，是调用次函数的对象传过来的，注意这种用法。
  handlerAvailableTime: function(e) {
    e.week_day = o.default.getWeekByDate(e.day),
      e.half_day = [{
        index: 0,
        availableFlag: !1,
        availableWeight: 0
      }, {
        index: 1,
        availableFlag: !1,
        availableWeight: 0
      }, {
        index: 2,
        availableFlag: !1,
        availableWeight: 0
      }, {
        index: 3,
        availableFlag: !1,
        availableWeight: 0
      }],
      e.hours.map(function(t, a) {
        a >= 0 && a <= 12 && !0 === t.is_show && (e.half_day[3].availableFlag = !0, e.half_day[3].availableWeight++),
          a > 12 && a <= 24 && !0 === t.is_show && (e.half_day[0].availableFlag = !0, e.half_day[0].availableWeight++),
          a > 24 && a <= 36 && !0 === t.is_show && (e.half_day[1].availableFlag = !0, e.half_day[1].availableWeight++),
          a > 36 && a <= 48 && !0 === t.is_show && (e.half_day[2].availableFlag = !0, e.half_day[2].availableWeight++);
      });
    console.log(e);
  },



})